package com.zilue.hfqueue.channel;

import java.io.IOException;

/**
 * Created by ZD on 2017/6/1.
 */
public class ChannelClient {
//    public final static ExecutorService executor = Executors.newFixedThreadPool(4);

    private static final String host="localhost";
    private static final int port1=8091;
    private static final int port2=8092;

    public static void main(String[] args) throws IOException {
        PostService postService = new PostService();
        for (int i = 0; i < 4; i++) {
            new Thread(() -> postService.postMessage()).start();
        }
    }

    static class PostService {
        void postMessage() {
            String  data="topic";
            for (int j = 0; j < 1000; j++) {
                data+=j;
            }
            long start = System.currentTimeMillis();
            for (int i = 0; i < 2000; i++) {
                try {

                    PostHandler.sendMessage("topic", data.getBytes(),host,port1);
//                    PostHandler.sendMessage("topic", data.getBytes(),host,port2);
                } catch (IOException e) {
                    System.err.println("异常线程:" + "" + Thread.currentThread().getId() + "__" + i + " reason:" + e.getMessage());
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("平均每次请求用时："+ Double.valueOf(end-start)/2000);
        }
    }
}

